package com.example.service.impl;

import com.example.mapper.GoodsMapper;
import com.example.pojo.Goods;
import com.example.pojo.PageBean;
import com.example.pojo.Result;
import com.example.service.GoodsService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class GoodsServiceImpl implements GoodsService {

    @Autowired
    private GoodsMapper goodsMapper;

    @Override
    public PageBean<Goods> list(Integer pageNum, Integer pageSize,String name, Integer cateId, String stuName, String state) {
        // 使用PageHelper开始分页
        PageHelper.startPage(pageNum, pageSize);
        // 查询数据
        List<Goods> goodsList = goodsMapper.list(name, cateId, stuName, state);
        // 获取分页信息
        Page<Goods> page = (Page<Goods>) goodsList;
        // 封装PageBean
        PageBean<Goods> pb = new PageBean<>();
        pb.setTotal(page.getTotal());
        pb.setItems(page.getResult());
        return pb;
    }

    @Override
    public Goods findById(Integer id) {
        return goodsMapper.findById(id);
    }

    @Override
    public void updateState(Goods goods) {
        // 设置更新时间
        goods.setUpdateTime(LocalDateTime.now());
        // 更新状态
        goodsMapper.update(goods);
    }
}